//
// export function saveToLocal(id, key, val) {
//   let seller = window.localStorage.__music__
//   if (!seller) {
//     seller = {}
//     seller[id] = {}
//   } else {
//     seller = JSON.parse(seller)
//     if (!seller[id]) {
//       seller[id] = {}
//     }
//   }
//   seller[id][key] = val
//   window.localStorage.__music__ = JSON.stringify(seller)
// }
//
// export function loadFromLocal(id, key, def) {
//   if (!id) return false
//   let seller = window.localStorage.__music__
//   if (!seller) return def
//   seller = JSON.parse(seller)
//   if (!seller[id]) return def
//   return seller[id][key]
// }
import {saveToLocal, loadFromLocal} from './apiLocalStorage'
import * as _ from 'lodash'

const NORMAL_LOCAL = 'NORMAL_LOCAL'
const SEARCH_HISTORY = 'SEARCH_HISTORY'
const PLAY_HISTORY = 'PLAY_HISTORY'
const MAX_HISTORY = 100
const FAVORITE_KEY = '__FAVORITE_KEY__'
const MAX_FAVORITE = 100

// --------------------------
export function loadSearch(def = []) {
  try {
    let ret = JSON.parse(
        loadFromLocal(NORMAL_LOCAL, SEARCH_HISTORY, '[]')
    )
    if (!ret || ret.length <= 0) return def
    return ret
  } catch (e) {
    return def
  }
}
export function saveSearch(val) { // 默认返回，缓存好的结果
  val = val.trim()
  try {
    let arr = loadSearch([])
    let repeatIndex = _.findIndex(arr, function(v) {
      return v === val
    })
    if (repeatIndex === 0) {
      return arr
    }
    if (repeatIndex > 0) {
      arr.splice(repeatIndex, 1)
    }
    arr.unshift(val)
    arr = arr.slice(0, 10)
    saveToLocal(NORMAL_LOCAL, SEARCH_HISTORY, JSON.stringify(arr))
    return arr
  } catch (e) {
    return []
  }
}
export function deleteSearch(val) { // 默认返回，缓存好的结果
  val = val.trim()
  try {
    let arr = loadSearch([])
    let repeatIndex = _.findIndex(arr, function(v) {
      return v === val
    })
    if (repeatIndex >= 0) {
      arr.splice(repeatIndex, 1)
      // 保存回 localStorage
      saveToLocal(NORMAL_LOCAL, SEARCH_HISTORY, JSON.stringify(arr))
    }
    return arr
  } catch (e) {
    return []
  }
}
export function clearSearch() { // 默认返回，缓存好的结果
  try {
    let arr = []
    saveToLocal(NORMAL_LOCAL, SEARCH_HISTORY, JSON.stringify(arr))
    return arr
  } catch (e) {
    return []
  }
}
// --------------------------
export function loadPlayHistory(def = []) {
  try {
    let ret = JSON.parse(
      loadFromLocal(NORMAL_LOCAL, PLAY_HISTORY, '[]')
    )
    if (!ret || ret.length <= 0) return def
    return ret
  } catch (e) {
    return def
  }
}

export function savePlayHistory(song) {
  try {
    let arr = loadPlayHistory([])
    let repeatIndex = _.findIndex(arr, function(item) {
      return song.mid === item.mid
    })
    if (repeatIndex === 0) {
      return arr
    }
    if (repeatIndex > 0) {
      arr.splice(repeatIndex, 1)
    }
    arr.unshift(song)
    if (arr.length > MAX_HISTORY) {
      arr = arr.slice(0, MAX_HISTORY)
    }
    saveToLocal(NORMAL_LOCAL, PLAY_HISTORY, JSON.stringify(arr))
    return arr
  } catch (e) {
    return []
  }
}
export function deletePlayHistory(song) { // 默认返回，缓存好的结果
  try {
    let arr = loadPlayHistory([])
    let repeatIndex = _.findIndex(arr, function(o) {
      return song.mid === o.mid
    })
    if (repeatIndex >= 0) {
      arr.splice(repeatIndex, 1)
      // 保存回 localStorage
      saveToLocal(NORMAL_LOCAL, PLAY_HISTORY, JSON.stringify(arr))
    }
    return arr
  } catch (e) {
    return []
  }
}
// --------------------------
export function loadFavoriteList(def = []) {
  try {
    let ret = JSON.parse(
      loadFromLocal(NORMAL_LOCAL, FAVORITE_KEY, '[]')
    )
    if (!ret || ret.length <= 0) return def
    return ret
  } catch (e) {
    return def
  }
}
export function saveFavoriteList(song) {
  try {
    let arr = loadFavoriteList([])
    let repeatIndex = _.findIndex(arr, function(item) {
      return song.mid === item.mid
    })
    if (repeatIndex === 0) {
      return arr
    }
    if (repeatIndex > 0) {
      arr.splice(repeatIndex, 1)
    }
    arr.unshift(song)
    if (arr.length > MAX_FAVORITE) {
      arr = arr.slice(0, MAX_FAVORITE)
    }
    saveToLocal(NORMAL_LOCAL, FAVORITE_KEY, JSON.stringify(arr))
    return arr
  } catch (e) {
    return []
  }
}

export function deleteFavoriteList(song) { // 默认返回，缓存好的结果
  try {
    let arr = loadFavoriteList([])
    let repeatIndex = _.findIndex(arr, function(o) {
      return song.mid === o.mid
    })
    if (repeatIndex >= 0) {
      arr.splice(repeatIndex, 1)
      // 保存回 localStorage
      saveToLocal(NORMAL_LOCAL, FAVORITE_KEY, JSON.stringify(arr))
    }
    return arr
  } catch (e) {
    return []
  }
}

export function clearFavoriteList() { // 默认返回，缓存好的结果
  try {
    let arr = []
    saveToLocal(NORMAL_LOCAL, FAVORITE_KEY, JSON.stringify(arr))
    return arr
  } catch (e) {
    return []
  }
}
